package com.chidopi.market.bookstore.service.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.chidopi.cdf.dao.IBaseDao;
import com.chidopi.cdf.exception.CdfException;
import com.chidopi.cdf.service.IBaseService;
import com.chidopi.market.bookstore.domain.Subscribe4iOS;
import com.chidopi.market.bookstore.domain.Subscriptions;


public class SubscriptionsServiceImpl implements IBaseService<Subscriptions> {
	
	private static Logger logger = LoggerFactory.getLogger(SubscriptionsServiceImpl.class);
	
	@Autowired
	@Qualifier("subscriptionsDao")
	private IBaseDao<Subscriptions> baseDao;
	
	@Autowired
	@Qualifier("subscribe4iOSDao")
	private IBaseDao<Subscribe4iOS> iOSDao;
	
	@Override
	public int insert(Subscriptions t) throws Exception {
		return this.baseDao.insert(t);
	}

	@Override
	public int update(Subscriptions t) throws Exception {
		return this.baseDao.update(t);
	}

	@Override
	public int delete(Subscriptions t) throws Exception {
		Integer id = t.getId();
		String sqlWhere = "a.SUBSCRIBE_ID = ?";
		List<Subscribe4iOS> ios = this.iOSDao.find(sqlWhere, id);
		if (ios.size() > 0) {
			logger.debug("delete [" + id + "] failure: Subscribe4iOS.size()=" + ios.size());
			throw new CdfException("error.subscription.delete.subscribe4ios");
		}
		return this.baseDao.delete(t);
	}

	@Override
	public List<Subscriptions> find(String sqlWhere, Object... args) throws Exception {
		logger.debug(sqlWhere);
		logger.debug(Arrays.asList(args).toString());
		return this.baseDao.find(sqlWhere, args);
	}

	@Override
	public Subscriptions findById(Object id) throws Exception {
		Subscriptions sub = this.baseDao.findById(id);
		
		return sub;
	}

	@Override
	public int getNextSequence() throws Exception {
		return 0;
	}

}
